package org.scuecsz.view.utils;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.io.Serializable;
import java.util.Random;

//定义类

public class VerifyCode implements Serializable{

    private static Random random = new Random();
    private static String ssource = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" + "abcdefghijklmnopqrstuvwxyz" + "0123456789";
    private String codes;
    private int width=0;
    private int height=0;

    public VerifyCode(){
    }



    public void getNewCodes(int length){
        char[] buf = new char[length];
        int rnd;

        for (int i = 0; i < length; i++) {
            rnd = Math.abs(random.nextInt(ssource.length()));
            buf[i] = ssource.charAt(rnd);
        }
        codes=new String(buf);
        width = 24 * length;
        height = 20;//图象宽度与高度
    }

    public  boolean hasCode() {
              return null!=codes&&!"".equals(this.codes);
    }

    public  boolean isVerifyCode(String codes,boolean isIgnoreCase) {
           if(isIgnoreCase){
                return codes.equalsIgnoreCase(this.codes);
           }else{
               return codes.equals(this.codes);
           }
    }



    public void paintVerifyCode(Graphics2D g) {
        int iLength = codes.length();//得到验证码长度

        int CharWidth = (int) (width - 24) / iLength;    //字符距左边宽度
        int CharHeight = 16;   //字符距上边高度

        g.setColor(getRandColor(200, 240));
        g.fillRect(0, 0, width, height);

        g.setColor(getRandColor(10, 50));
        g.drawRect(0, 0, width - 1, height - 1);

        for (int i = 0; i < 2 * width; i++) {
            int x = random.nextInt(width);
            int y = random.nextInt(height);
            int xl = random.nextInt(12);
            int yl = random.nextInt(12);
            g.setColor(getRandColor(160, 200));
            g.drawLine(x, y, x + xl, y + yl);
        }

        for (int i = 0; i < iLength; i++) {
            String code = codes.substring(i, i + 1);
            g.setFont(new Font("Arial Black", Font.PLAIN, 15 + random.nextInt(5)));
            g.setColor(new Color(20 + random.nextInt(60), 20 + random.nextInt(120), 20 + random.nextInt(180)));
            g.drawString(code, CharWidth * i + 14, CharHeight);
        }

        for (int i = 0; i < 5 * iLength; i++) {
            int x = random.nextInt(width);
            int y = random.nextInt(height);
            int xl = random.nextInt(12);
            int yl = random.nextInt(12);
            g.setColor(getRandColor(160, 200));
            g.drawLine(x, y, x + xl, y + yl);
        }
    }


    //给定范围获得随机颜色
    private Color getRandColor(int fc, int bc) {
        if (fc > 255) {
            fc = 255;
        }
        if (bc > 255) {
            bc = 255;
        }
        int r = fc + random.nextInt(bc - fc);
        int g = fc + random.nextInt(bc - fc);
        int b = fc + random.nextInt(bc - fc);
        return new Color(r, g, b);
    }

    public int getHeight() {
        return height;
    }

    public void setHeight(int height) {
        this.height = height;
    }

    public int getWidth() {
        return width;
    }

    public void setWidth(int width) {
        this.width = width;
    }
}

